import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class RedisDemoHash {
    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        try(Jedis jedis = jedisPool.getResource()){
            //test1(jedis);
            //test2(jedis);
            //test3(jedis);
            //test4(jedis);
            test5(jedis);
        }
    }

    private static void test5(Jedis jedis) {
        System.out.println("hmget hmset");
        jedis.flushAll();
        Map<String, String> map = new HashMap<>();
        map.put("f1", "111");
        map.put("f2", "222");
        map.put("f3", "333");
        jedis.hmset("key1", map);
        List<String> list = jedis.hmget("key1", "f1", "f2", "f3");
        System.out.println(list);

    }

    private static void test4(Jedis jedis) {
        System.out.println("hkeys hvals");
        jedis.flushAll();
        jedis.hset("key1", "f1", "111");
        jedis.hset("key1", "f2", "222");
        jedis.hset("key1", "f3", "333");
        Set<String> set = jedis.hkeys("key1");
        System.out.println(set);
        List<String> list = jedis.hvals("key1");
        System.out.println(list);
    }

    private static void test3(Jedis jedis) {
        System.out.println("hdel");
        jedis.flushAll();
        jedis.hset("key1", "f1", "111");
        jedis.hset("key1", "f2", "222");
        jedis.hset("key1", "f3", "333");
        long result = jedis.hdel("key1", "f1");
        boolean b = jedis.hexists("key1", "f1");
        System.out.println(result + " " + b);
    }

    private static void test2(Jedis jedis) {
        System.out.println("hexists");
        jedis.flushAll();
        jedis.hset("key1", "f1", "111");
        jedis.hset("key1", "f2", "222");
        jedis.hset("key1", "f3", "333");
        boolean result = jedis.hexists("key1", "f1");
        System.out.println(result);
        result = jedis.hexists("key1", "f10");
        System.out.println(result);

    }

    private static void test1(Jedis jedis) {
        System.out.println("hset hget");
        jedis.flushAll();
        jedis.hset("key1", "f1", "111");
        Map<String, String> map = new HashMap<>();
        map.put("f2", "222");
        map.put("f3", "333");
        jedis.hset("key1", map);
        String result = jedis.hget("key1","f1");
        System.out.println(result);
        result = jedis.hget("key1", "f2");
        System.out.println(result);
        result = jedis.hget("key1", "f3");
        System.out.println(result);

    }
}
